﻿using GTI = Gadgeteer.Interfaces;
using GTM = Gadgeteer.Modules;

namespace Gadgeteer.Modules.DFRobot
{
    /// <summary>
    /// A UV Sensor module for Microsoft .NET Gadgeteer
    /// </summary>
    public class UV_Sensor : GTM.Module
    {
        // This example implements a driver in managed code for a simple Gadgeteer module.  This module uses a 
        // single GTI.InterruptInput to interact with a button that can be in either of two states: pressed or released.
        // The example code shows the recommended code pattern for exposing a property (IsPressed). 
        // The example also uses the recommended code pattern for exposing two events: Pressed and Released. 
        // The triple-slash "///" comments shown will be used in the build process to create an XML file named
        // GTM.DFRobot.$HardwareName$. This file will provide IntelliSense and documentation for the
        // interface and make it easier for developers to use the $HardwareName$ module.        

        // -- CHANGE FOR MICRO FRAMEWORK 4.2 --
        // If you want to use Serial, SPI, or DaisyLink (which includes GTI.SoftwareI2C), you must do a few more steps
        // since these have been moved to separate assemblies for NETMF 4.2 (to reduce the minimum memory footprint of Gadgeteer)
        // 1) add a reference to the assembly (named Gadgeteer.[interfacename])
        // 2) in GadgeteerHardware.xml, uncomment the lines under <Assemblies> so that end user apps using this module also add a reference.

        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public UV_Sensor(int socketNumber)
        {
            // This finds the Socket instance from the user-specified socket number.  
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter, 
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            socket.EnsureTypeIsSupported('A', this);

            this.input = new GTI.AnalogInput(socket, Socket.Pin.Four, this);
        }

        private GTI.AnalogInput input;

        /// <summary>
        /// Returns the current voltage reading of the UV sensor
        /// </summary>
        public double ReadUVSensorVoltage()
        {
            return input.ReadVoltage();
        }

        /// <summary>
        /// Returns the current UV Level reading of the UV sensor
        /// </summary>
        public double GetUVI()
        {
            double volts = ReadUVSensorVoltage();

            double UVI = (((volts * 3300) / 4.3) - 45) / 27.0;

            if (UVI < 0)
                return 0.0;
            else
                return UVI;
        }
    }
}
